package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.*;
import com.example.pojo.*;
import com.example.service.IResignService;
import com.example.vo.ResultVo;
import com.example.vo.per.EmpTurnover;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 离职申请表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-05-05
 */
@Service
public class ResignServiceImpl extends ServiceImpl<ResignMapper, Resign> implements IResignService {

    @Autowired
    private ResignMapper rm;
    @Autowired
    private EmpMapper em;
    @Autowired
    private ResumeMapper rem;
    @Autowired
    private JobDemandMapper jdm;

    @Override
    public Integer addRes(Resign resign) {
        resign.setDate(LocalDate.now());
        resign.setProcessId(9);
        resign.setCreateTime(LocalDateTime.now());
        return rm.insert(resign);
    }

    @Override
    public PageInfo<EmpTurnover> selResByEmpIdList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<EmpTurnover> list = em.selectA();
        PageInfo<EmpTurnover> empTurnoverPageInfo = new PageInfo<>(list);
        List<EmpTurnover> list1 = empTurnoverPageInfo.getList();
        for (EmpTurnover empTurnover : list1) {
            Resign resign = rm.selectByEmpId(empTurnover.getEmpId());
            empTurnover.setResign(resign);
        }
        return empTurnoverPageInfo;
    }

    @Override
    @Transactional
    public ResultVo delEmp(Integer[] ids) {
        for (Integer id : ids) {
            Resign resign = new Resign();
            resign.setId(id);
            resign.setStatus(1);
            rm.updateById(resign);

            Resign r = rm.selectById(resign);

            if (r.getEmpId() != null){
                System.err.println("--> " + r.getEmpId());
                Emp emp = new Emp();
                emp.setId(r.getEmpId());
                emp.setUpdateTime(LocalDateTime.now());
                emp.setStatus(0);
                emp.setDeleted(1);
                em.updateById(emp);

                JobDemand jobDemand = jdm.selectOne(new QueryWrapper<JobDemand>().eq("position_id", emp.getPositionId()));
                jobDemand.setNum(jobDemand.getNum() + 1);
                jdm.updateById(jobDemand);

                Emp e = em.selectById(emp);
                if (e.getResumeId() != null){
                    System.err.println("--> " + e.getResumeId());
                    rem.updateById(new Resume()
                            .setId(e.getResumeId())
                            .setDeleted(1));
                }
            }
        }
        return ResultVo.ok();
    }

    @Override
    public ResultVo uptEmp(Integer[] ids) {
        for (Integer id : ids) {
            Resign resign = new Resign();
            resign.setId(id);
            resign.setStatus(2);
            rm.updateById(resign);
        }
        return ResultVo.ok();
    }
}
